package com.piggy.settlement.sdk.client.batchimportform;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.PiggyBatchImportFormConstants;
import com.piggy.settlement.sdk.constant.PiggySdkConstants;
import com.piggy.settlement.sdk.util.PiggyVerifyUtil;
import com.piggy.settlement.sdk.util.PiggyXmlUtil;

/**
 * @author ：daxia
 * @description：批量上报相关接口
 * @modified By：
 * @version: 1.0$
 */
public class PiggyBatchImportFormApiClient {


    static PiggyBatchImportFormApiClient piggyBatchImportFormApiClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggyBatchImportFormApiClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggyBatchImportFormApiClient getInstanse(String appSecret, String domain) {
        if (piggyBatchImportFormApiClient == null) {
            piggyBatchImportFormApiClient =
                    new PiggyBatchImportFormApiClient(appSecret, domain);
        }

        return piggyBatchImportFormApiClient;
    }


    /**
     * 查询税源地公司信息
     * 该方法已经过时，请使用v2相关接口- getTaxFundList
     *
     * @param batchImportForm 参数实体
     * @return
     */
    @Deprecated
    public JSONObject getSubsidiaryList(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.GET_SUBSIDIARY_LIST_V1 + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪查询税源地公司信息错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询资金池余额
     * 该方法已经过时，请使用v2相关接口- getCashPooling
     *
     * @param batchImportForm 参数实体
     * @return
     */
    @Deprecated
    public JSONObject getCashPooling(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.GET_CASH_POOLING + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪查询资金池余额信息错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 批量上报-服务费导入
     *
     * @param batchImportForm 参数实体
     * @return
     */
    public JSONObject importForm(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.IMPORT_FORM_V1 + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪批量上报-服务费导入错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 批量上报-服务费发放接口
     *
     * @param batchImportForm 参数实体
     * @return
     */
    public JSONObject sendCashForOpen(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.SEND_CASH_FOR_OPEN + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪批量上报-服务费发放错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 批量上报-查询发放服务费明细
     *
     * @param batchImportForm 参数实体
     * @return
     */
    public JSONObject getCashDetailForOpen(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.GET_CASH_DETAIL_FOR_OPEN + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪批量上报-查询发放服务费明细错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 批量上报-查询对账明细
     *
     * @param batchImportForm 参数实体
     * @return
     */
    public JSONObject getReconcileTradesDetail(Object batchImportForm) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, batchImportForm);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(batchImportForm);

            String xml = PiggyXmlUtil.objectToXml(batchImportForm);
            String response = HttpRequest.post(domain + PiggyBatchImportFormConstants.GET_RECONCILE_TRADES_DETAIL + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪批量上报-查询对账明细错误");
            e.printStackTrace();
            return null;
        }

    }

    private JSONObject getJsonObject(String response) {
        if (!StrUtil.hasBlank(response) && response.startsWith(PiggySdkConstants.JSON_STR_START)) {
            return JSONUtil.parseObj(response);
        } else {
            return JSONUtil.parseFromXml(response);
        }
    }

}
